package cn.demomaster.quickui.service;

import android.content.Context;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.demomaster.quickcompat.service.QuickFloatingService;
import com.demomaster.quickcompat.util.QuickThread;

import cn.demomaster.quickrouter.manager.QuickActivityManager;
import cn.demomaster.quickui.R;
import cn.demomaster.quickui.util.FPSMonitor;

/**
 *  悬浮实时Fps
 */
public class FpsFloatingService extends QuickFloatingService {
    LinearLayout linearLayout;
    
    @Override
    public void onCreateView(Context context, WindowManager windowManager) {
        linearLayout = new LinearLayout(context.getApplicationContext());
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        TextView button = new TextView(context);
        button.setText("fps");
        button.setPadding(10, 10, 10, 10);
        button.setBackgroundColor(context.getResources().getColor(com.demomaster.quickcompat.R.color.transparent_dark_99));
        button.setTextColor(context.getResources().getColor(com.demomaster.quickcompat.R.color.white));
        fpsMonitor.setOnFramChangedListener(mframe -> {
            frame = mframe;
            if (QuickActivityManager.getInstance().getCurrentActivity() != null) {
                Display display = QuickActivityManager.getInstance().getCurrentActivity().getWindowManager().getDefaultDisplay();
                float refreshRate = display.getRefreshRate();
                QuickThread.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        button.setText("frame:" + frame);
                        ///QdLog.i("rate:"+refreshRate);
                    }
                });
            }
        });
        fpsMonitor.start();

//        layoutParams = new WindowManager.LayoutParams();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;//TYPE_APPLICATION_OVERLAY;
//        } else {
//            //layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
//            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//        }
//        layoutParams.format = PixelFormat.RGBA_8888;
//        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
//        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//        if (!getTouchAble()) {
//            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//        }

        linearLayout.addView(button);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        addView(linearLayout, layoutParams, true, 100, 100);
    }

//    @Override
//    public PointF getOriginPoint() {
//        return new PointF(100, 100);
//    }

    float frame = 0;
    FPSMonitor fpsMonitor = new FPSMonitor();

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeView(linearLayout);
        if (fpsMonitor != null)
            fpsMonitor.stop();
    }
}
